<?php
require_once "PHPExcel.php";


header('Content-Type:text/html; charset=utf-8');//转换一下格式  避免乱码
$objExcel = new \PHPExcel();
//set document Property
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$letter =explode(',',"A,B,C,D,E,F,G,H,I,J");
//填充表头信息
$arrHeader = array('序号',"类型",'名字','性别','身份证号','代表队','代表队简称',"组别","项目","联系方式");

$lenth =  count($arrHeader);
for($i = 0;$i < $lenth;$i++) {
	$objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
};

$arr = [
	[
		'id' => 1,
		'name' => 'wyq',
		'nickname' => '心如止水',
		'password' => '123456',
		'phone' => '13451167651',
		'init_time' => 1622632002,
	],
	[
		'id' => 2,
		'name' => 'fj',
		'nickname' => 'xj',
		'password' => '123456',
		'phone' => '13451163651',
		'init_time' => 1622632002,
	]
];

//填充表格信息
foreach($arr as $k=>$v){
	$k +=2;

	$objActSheet->setCellValue('A'.$k,$v['id']);
	$objActSheet->setCellValue('B'.$k, $v['nickname']);
	$objActSheet->setCellValue('C'.$k, $v['name']);
	$objActSheet->setCellValue('D'.$k, $v['password']);
	$objActSheet->setCellValue('E'.$k, $v['phone']." "."\t");
	$objActSheet->setCellValue('F'.$k, $v['init_time']);

	// 表格高度
	$objActSheet->getRowDimension($k)->setRowHeight(20);
}

$width = array(10,15,20,25,30);
//设置表格的宽度
$objActSheet->getColumnDimension('A')->setWidth($width[1]);
$objActSheet->getColumnDimension('B')->setWidth($width[3]);
$objActSheet->getColumnDimension('C')->setWidth($width[2]);
$objActSheet->getColumnDimension('D')->setWidth($width[4]);
$objActSheet->getColumnDimension('E')->setWidth($width[4]);
$objActSheet->getColumnDimension('F')->setWidth($width[4]);
$objActSheet->getColumnDimension('G')->setWidth($width[4]);
$objActSheet->getColumnDimension('H')->setWidth($width[4]);
$objActSheet->getColumnDimension('I')->setWidth($width[3]);
$objActSheet->getColumnDimension('J')->setWidth($width[4]);

//下载的文件夹名
$outfile = "result.xlsx";
$path='./';//upload为目标文件夹

if (!is_dir($path)){
	$oldumask = umask(0);
	mkdir($path,0777,true);
	umask($oldumask);
}

$objWriter->save(str_replace('.php','.xls',$path.$outfile));


print_r("----------------------\n");
?>
